package com.tuling.learnjuc.demo;

import com.tuling.learnjuc.forkjoin.util.Utils;

import java.util.PriorityQueue;

public class Top100LargestNumbers {
    public static int[] findTop100LargestNumbers(int[] nums) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(100);

        for (int num : nums) {
            if (minHeap.size() < 100) {
                minHeap.offer(num);
            } else if (num > minHeap.peek()) {
                minHeap.poll();
                minHeap.offer(num);
            }
        }

        int[] result = new int[100];
        for (int i = 0; i < 100; i++) {
            result[i] = minHeap.poll();
        }

        return result;
    }

    public static void main(String[] args) {
        // 1亿个数据
        int[] nums = Utils.buildRandomIntArray(100000000);
        int[] top100 = findTop100LargestNumbers(nums);
        for (int num : top100) {
            System.out.println(num);
        }
    }
}